/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.tool;

import com.funcom.peeler.WorkBench;
import com.funcom.peeler.component.WorkSpace;
import com.funcom.peeler.peelable.PeelCanvasWindow;
import com.funcom.peeler.peelable.PeelComponent;
import com.funcom.peeler.utils.SelectorBox;
import com.jmex.bui.BComponent;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Set;

public class SelectionBoxTool extends Tool {

    SelectorBox box = new SelectorBox();
    Set< PeelComponent > non_box_selections = new HashSet< PeelComponent >();

    public SelectionBoxTool( WorkBench workbench ) {
        super( workbench );
        bui.getSelectionLayer().getComponent().add( box );
    }

    public void mousePressed( MouseEvent e ) {

        switch( e.getButton() ) {

            case MouseEvent.BUTTON1: {

                if ( e.isControlDown() ) non_box_selections.addAll( bui.getSelections() );

                box.start.move( e.getX(), e.getY() );
                box.end.move( e.getX(), e.getY() );
            }
        }
    }

    public void mouseDragged( MouseEvent e ) {

        bui.clearSelections();

        for( PeelComponent component : non_box_selections ) {
            bui.select( component );
        }

        if ( box.start.getX() != -1 )
            box.end.move( e.getX(), e.getY() );

        for ( PeelComponent component : getHitComponents() ) {
            bui.select( component );
        }
    }

    public void mouseReleased( MouseEvent e ) {

        non_box_selections.clear();

        box.start.move( -1, -1 );
        box.end.move( -1, -1 );

        if ( e.getButton() == MouseEvent.BUTTON1 )
            workbench.popTool();
    }

    private Set< PeelComponent > getHitComponents() {

        HashSet< PeelComponent > hitComponents = new HashSet< PeelComponent >();

        Rectangle check_box = getAdjustedBox();

        for ( PeelComponent component : bui.getAllComponents() ) {

            final com.jmex.bui.util.Rectangle bounds = component.getComponent().getBounds();
            Rectangle check_bounds = new Rectangle( bounds.x, bounds.y, bounds.width, bounds.height );

            BComponent parent = component.getComponent().getParent();
            while ( parent != null && !(parent instanceof WorkSpace) ) {
                check_bounds.x += parent.getX();
                check_bounds.y += parent.getY();

                parent = parent.getParent();
            }

            // Only select components that are contained within the selection box and don't touch the canvas window
            if ( check_box.contains( check_bounds ) && !( component instanceof PeelCanvasWindow) )
                hitComponents.add( component );
        }

        return hitComponents;
    }

    private Rectangle getAdjustedBox() {

        Rectangle new_box =  new Rectangle( Math.min( box.start.x, box.end.x ), Math.min( box.start.y, box.end.y ),
                Math.max( box.start.x, box.end.x ) - Math.min( box.start.x, box.end.x ),
                Math.max( box.start.y, box.end.y ) - Math.min( box.start.y, box.end.y ) );
        new_box.translate( -bui.getWorkSpace().getComponent().getX(), -bui.getWorkSpace().getComponent().getY() );
        return new_box;
    }
}
